#include "weather.h"
#include "weather_gui.h"

#include "../sys/app_contorller.h"
#include "../../network.h"
#include <NTPClient.h> 
#include <WiFiUdp.h> 
#include "../../common.h"

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "ntp.aliyun.com");



#define WEATHER_PAGE_SIZE 3

int clock_page = 0;                           // 时钟桌面的播放记录int clock_page = 0;                           // 时钟桌面的播放记录
unsigned long preWeatherMillis = 0;           // 上一回更新天气时的毫秒数
unsigned long preTimeMillis = 0;              // 上一回从网络更新日期与时间时的毫秒数
unsigned long weatherUpdataInterval = 900000; // 天气更新的时间间隔
unsigned long timeUpdataInterval = 300000;    // 日期时钟更新的时间间隔(300s)
unsigned long epochTime = timeClient.getEpochTime();
String adjDigit(int number){
  if (number >= 10){
    return String(number);
  } else {
    return String("0" + String(number));
  }
}

void UpdateWeather(lv_scr_load_anim_t anim_type)
{   
    
String timeString,dateString;
 
int time2= 0;


  if (millis() - preWeatherMillis >= weatherUpdataInterval)
     {
      
     // mweather = g_network.getTianqi("https://api.seniverse.com/v3/weather/now.json?key=Sd8al_0pDThjCwtJG&location=Dongguan&language=zh-Hans&unit=c");
     // tiangi = String(mweather.temperature);
      //fayl = String( g_network.getBilibiliFans("http://api.bilibili.com/x/relation/stat?vmid=20259914"));   
      //Serial.println(mweather.temperature);
     
      timeString = String(timeClient.getHours()) + ":" + adjDigit(timeClient.getMinutes());
      unsigned long epochTime = timeClient.getEpochTime();
      struct tm *ptm = gmtime ((time_t *)&epochTime);
      int monthDay = ptm->tm_mday;
      int tm_Month = ptm->tm_mon+1;
      int tm_Year = ptm->tm_year+1900;
      dateString = adjDigit(tm_Year) + "-" + adjDigit(tm_Month)+ "-"+adjDigit(monthDay);
      time2 = timeClient.getDay();

    }
     display_weather(timeString.c_str(), dateString.c_str(), time2,anim_type);

}

void UpdateTime_RTC(lv_scr_load_anim_t anim_type)
{
    
   

    String fayl,tiangi;
 if (millis() - preWeatherMillis >= timeUpdataInterval)
     {
      Weather mweather;
      mweather = g_network.getTianqi("https://api.seniverse.com/v3/weather/now.json?key=Sd8al_0pDThjCwtJG&location=Dongguan&language=zh-Hans&unit=c");
      fayl = String( g_network.getBilibiliFans("http://api.bilibili.com/x/relation/stat?vmid=20259914"));   
      tiangi = String(mweather.temperature);
    }



    display_time(tiangi.c_str(),fayl.c_str(),anim_type);
}

           
void Upday(lv_scr_load_anim_t anim_type){

   
    
    unsigned long epochTime1 = timeClient.getEpochTime();
    struct tm *ptm1 = gmtime ((time_t *)&epochTime1);
    int monthDay1 = ptm1->tm_mday;
    int tm_Month1 = ptm1->tm_mon+1;
    int tm_Year1 = ptm1->tm_year+1900;

        display_hardware(tm_Year1,tm_Month1,monthDay1, anim_type);

}
           


void weather_init(void)
{
    weather_gui_init();
    timeClient.begin();
    timeClient.setTimeOffset(28800);  
  
    // 变相强制更新
    preWeatherMillis = millis() - weatherUpdataInterval;
    preTimeMillis = millis() - timeUpdataInterval;
}

void weather_process(AppController *sys,
                     const Imu_Action *act_info)
{     timeClient.update();
    
    lv_scr_load_anim_t anim_type = LV_SCR_LOAD_ANIM_NONE;
    if (RETURN == act_info->active)
    {
        sys->app_exit();
        return;
    }

    if (TURN_RIGHT == act_info->active)
    {
        // 切换界面时，变相强制更新
        preWeatherMillis = millis() - weatherUpdataInterval;
        preTimeMillis = millis() - timeUpdataInterval;
        anim_type = LV_SCR_LOAD_ANIM_MOVE_RIGHT;
        clock_page = (clock_page + 1) % WEATHER_PAGE_SIZE;
    }
    else if (TURN_LEFT == act_info->active)
    {
        // 切换界面时，变相强制更新
        preWeatherMillis = millis() - weatherUpdataInterval;
        preTimeMillis = millis() - timeUpdataInterval;
        anim_type = LV_SCR_LOAD_ANIM_MOVE_LEFT;
        // 以下等效与 clock_page = (clock_page + WEATHER_PAGE_SIZE - 1) % WEATHER_PAGE_SIZE;
        // +3为了不让数据溢出成负数，而导致取模逻辑错误
        clock_page = (clock_page + WEATHER_PAGE_SIZE - 1) % WEATHER_PAGE_SIZE;
    }

    if (0 == clock_page) // 更新天气
    {
        UpdateWeather(anim_type);
    }

    if (1 == clock_page) // 更新时钟
    {
        UpdateTime_RTC(anim_type);
    }

    if (2 == clock_page) // NULL后期可以是具体数据
    {
        
        Upday(anim_type);
        
    }
    delay(300);
}

void weather_exit_callback(void)
{
    weather_gui_del();
}

void weather_event_notification(APP_EVENT event)
{
    if (event == APP_EVENT_WIFI_CONN)
    {
    }
}



APP_OBJ weather_app = {"Weather", &app_weather, weather_init,
                       weather_process, weather_exit_callback,
                       weather_event_notification};